package com.jlastu.znb.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jlastu.znb.R;
import com.jlastu.znb.contract.MainContract;
import com.jlastu.znb.contract.MineContract;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.gwc.CartManager;
import com.jlastu.znb.presenter.MainPresenter;
import com.jlastu.znb.utils.SpUtils;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mTabOne;
    private LinearLayout mTabTwo;
    private LinearLayout mTabAdd;
    private LinearLayout mTabMessage;
    private LinearLayout mTabMine;
    private FirstFragment mFirstFragment;
    private MineFragment mMineFragment;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;
    private SchoolFragment mSchoolFragment;
    private MessageFragment mMessageFragment;
    private CartManager cartManager;
    private SpUtils mSpUtils;
    private MineContract.IMinePresenter mMinePresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cartManager = new CartManager(this);
        mSpUtils = new SpUtils(this);

        mTabOne = findViewById(R.id.ll_tab_one);
        mTabTwo = findViewById(R.id.ll_tab_two);
        mTabAdd = findViewById(R.id.ll_tab_add);
        mTabMessage = findViewById(R.id.ll_tab_message);
        mTabMine = findViewById(R.id.ll_tab_mine);

        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabAdd.setOnClickListener(this);
        mTabMessage.setOnClickListener(this);
        mTabMine.setOnClickListener(this);
        initFragment();
        changeFragment(mFirstFragment);
        mFragmentManager.beginTransaction()
                .add(R.id.fragment_content, mFirstFragment).commitAllowingStateLoss();
        mContentFragment = mFirstFragment;
        mTabOne.setSelected(true);
        if (isUserLoggedIn()) {
            showMineFragment();
        } else {
            // 如果用户未登录，可以在这里处理，例如提示用户登录
        }

    }

    private boolean isUserLoggedIn() {
        SpUtils mSpUtils = new SpUtils(this);
        return mSpUtils.getInt(SpUtils.LOGIN_KEY, -1) != -1;
    }

    private void showMineFragment() {
        MineFragment mineFragment = new MineFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_content, mFirstFragment);
        transaction.commit();
    }
    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mFirstFragment = new FirstFragment();
        mMineFragment = new MineFragment();
        mSchoolFragment = new SchoolFragment();
        mMessageFragment = new MessageFragment();
    }

    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .add(R.id.fragment_content, to).commitAllowingStateLoss();
            } else {
                mFragmentManager.beginTransaction().hide(mContentFragment)
                        .show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_tab_one) {
            mTabOne.setSelected(true);
            mTabTwo.setSelected(false);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(false);
            changeFragment(mFirstFragment);
        } else if (id == R.id.ll_tab_two) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(true);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(false);
            changeFragment(mSchoolFragment);

        } else if (id == R.id.ll_tab_add) {
            Intent intent = new Intent(MainActivity.this, AddGoodsActivity.class);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(
                    MainActivity.this).toBundle());
        } else if (id == R.id.ll_tab_message) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(false);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(true);
            mTabMine.setSelected(false);
            changeFragment(mMessageFragment);
        } else if (id == R.id.ll_tab_mine) {
            mTabOne.setSelected(false);
            mTabTwo.setSelected(false);
            mTabAdd.setSelected(false);
            mTabMessage.setSelected(false);
            mTabMine.setSelected(true);
            changeFragment(mMineFragment);
        }else if (id == R.id.sell111) {
            SellGoodsFragment sellGoodsFragment = new SellGoodsFragment();
          changeFragment(sellGoodsFragment);
            LinearLayout schoolButton = findViewById(R.id.ll_tab_two); // 假设ll_tab_school是“学堂”按钮的ID
            TextView schoolTextView = schoolButton.findViewById(R.id.tv_tab_two); // 假设按钮内部有一个TextView用于显示文本
            schoolTextView.setText("农友圈"); // 设置新的文本

            // 为“农友圈”设置点击事件
            schoolButton.setOnClickListener(v1 -> {
                // 这里可以添加跳转到另一个页面的逻辑
                // 例如，跳转到另一个Activity
                Intent intent1 = new Intent(MainActivity.this, com.jlastu.znb.firendcircle.MainActivity.class); // 假设AnotherActivity是目标Activity
                startActivity(intent1);
            });
        }
    }
    public void changeSchoolButtonTextToOriginal() {
        LinearLayout schoolButton = findViewById(R.id.ll_tab_two);
        TextView schoolTextView = schoolButton.findViewById(R.id.tv_tab_two);
        // 将文本改回“学堂”
        schoolTextView.setText("学堂");
        changeFragment(mFirstFragment);
        schoolButton.setOnClickListener(v1 -> {
            mTabTwo.setSelected(true);
            mTabOne.setSelected(false);
           changeFragment(mSchoolFragment);
        });
    }
    public void onAddToCartClick(int goodsId, int quantity) {
        cartManager.addToCart(goodsId, quantity, getUserId());
    }

    private int getUserId() {
        return mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
    }
}